/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.utils;

import com.ice.jni.registry.NoSuchKeyException;
import com.ice.jni.registry.NoSuchValueException;
import com.ice.jni.registry.Registry;
import com.ice.jni.registry.RegistryException;
import com.ice.jni.registry.RegistryKey;
import com.ice.jni.registry.RegistryValue;
import java.util.Enumeration;

/**
 *
 * @author root
 */
public class RegistryController {

    public static String[] getPnpIdFromName(String nameOfInterface) throws
            NoSuchValueException,
            RegistryException,
            Exception {

        String retStr[] = new String[2];
        String pnpDeviceStr = null;
        String keyPathForWpa = null;
        RegistryKey networksKey = findNicCardsKey();
        Enumeration subKeys = networksKey.keyElements();
        while (subKeys.hasMoreElements()) {

            RegistryKey keyInCharge =
                    networksKey.openSubKey((String) subKeys.nextElement());
            RegistryKey networkIds = null;
            RegistryValue nicNameValue = null;
            try {

                networkIds = keyInCharge.openSubKey("Connection");
                nicNameValue = networkIds.getValue("Name");
                String foundNicStrs = new String(nicNameValue.getByteData());
                if (nameOfInterface.equals(foundNicStrs)) {
                    keyPathForWpa = keyInCharge.getName();
                    pnpDeviceStr =
                            new String(networkIds.getValue("PnpInstanceID").
                            getByteData());
                    if (DevconUtils.isDeviceExist(pnpDeviceStr) && (DevconUtils.isDeviceRunning(pnpDeviceStr) ||
                            DevconUtils.isDeviceDisabled(pnpDeviceStr))) {
                        break;
                    } else {
                        keyPathForWpa = null;
                        pnpDeviceStr = null;
                    }
                }
            } catch (NoSuchKeyException ex) {
            } catch (NoSuchValueException ex) {
            }
        }

        if (pnpDeviceStr == null) {
            return null;
        }
        retStr[0] = pnpDeviceStr;
        retStr[1] = keyPathForWpa;
        return retStr;

    }

    private static RegistryKey findNicCardsKey() throws RegistryException {

        String rootKey = "SYSTEM\\ControlSet001\\Control\\Network";
        RegistryKey hKeyLocalMach = Registry.HKEY_LOCAL_MACHINE;
        RegistryKey networkKey = Registry.openSubkey(hKeyLocalMach,
                rootKey,
                RegistryKey.ACCESS_READ);
        Enumeration subKeys = networkKey.keyElements();
        RegistryKey retKey = null;
        while (subKeys.hasMoreElements()) {
            String subString = rootKey + "\\" + (String) subKeys.nextElement();
            retKey = Registry.openSubkey(hKeyLocalMach,
                    subString,
                    RegistryKey.ACCESS_READ);
            RegistryValue classValue = null;
            try {
                classValue = retKey.getValue("Class");
                String classString = new String(classValue.getByteData());
                if (classString.equals("Net")) {
                    break;
                }
            } catch (NoSuchValueException ex) {
            }

        }

        if (retKey == null) {
            throw new NoSuchValueException("Cant find value");
        }
        return retKey;

    }
}
